<template>
	<ele-form v-if="type != 'video'" v-model="value" class="ele-form-upload" :form-desc="formDesc" :is-show-back-btn="false" :is-show-submit-btn="false" />
	<vUploadVideo @upload-success="$emit('upload-success', $event)" v-model="value" :name="name" :returnType="returnType" :size="size" :limit="limit" v-else></vUploadVideo>
</template>
<script>
import vUploadVideo from "./upload-video.vue"

export default {
	name: "FortressUpload",
	model: {
		prop: "formData",
		event: "update:formData",
	},
	props: {
		type: {
			type: String,
			default: "image",
		},
		name: {
			type: String,
		},
		formData: {
			type: Object,
			default: () => ({}),
		},
		size: {
			type: Number,
		},
		multiple: {
			type: Boolean,
			default: false,
		},
		limit: {
			type: [Number, String],
			default: 5,
		},
		returnType: {
			type: String,
			default: "array", // json string array
		},
		isShowTip: {
			type: Boolean,
			default: true, // json string array
		},
	},
	data() {
		return {
			formValue: this.name,
			formDesc: {},
		}
	},
	computed: {
		value: {
			get() {
				return this.formData
			},
			set(value) {
				if (this.returnType == "json") {
					value[this.name] = JSON.stringify(value[this.name])
				} else if (this.returnType == "string") {
					value[this.name] = value[this.name].join(",")
				}
				this.$emit("update:formData", value)
			},
		},
	},
	created() {
		const that = this
		if (this.type == "file") {
			this.formDesc[this.name] = {
				type: "upload-file",
				attrs: {
					fileSize: this.size ? this.size : 10,
					isShowTip: this.isShowTip,
					name: "upfile",
					multiple: this.multiple,
					limit: this.limit,
					action: window.$conf.commonConf.baseUrl + "/system/oss/upload",
					isCanDownload: false,
					responseFn(response, file) {
						that.$emit("upload-success", [response, file])

						return {
							name: file.name,
							url: response.data.url,
						}
					},
				},
			}
		} else if (this.type == "video") {
			this.formDesc[this.name] = {
				type: "video-uploader",
				attrs: {
					fileSize: this.size ? this.size : 20,
					action: window.$conf.commonConf.baseUrl + "/system/oss/upload",
					// 上传的文件字段名 (同官网)
					name: "upfile",
					responseFn(response, file) {
						that.$emit("upload-success", [response, file])
						return response.data.url
					},
				},
			}
		} else if (this.type == "image") {
			this.formDesc[this.name] = {
				type: "image-uploader",
				attrs: {
					fileSize: this.size ? this.size : 3,
					action: window.$conf.commonConf.baseUrl + "/system/oss/upload",
					// 上传的文件字段名 (同官网)
					name: "upfile",
					multiple: this.multiple,
					limit: this.limit,
					drag: true,
					responseFn(response, file) {
						that.$emit("upload-success", [response, file])
						return response.data.url
					},
				},
			}
		} else if (this.type == "videoShow") {
			this.formDesc[this.name] = {
				type: "gallery",
				default: this.videoSrc,
				attrs: {
					type: "video",
					size: this.size ? this.size : 100,
				},
			}
		}
	},
	components: {
		vUploadVideo,
	},
}
</script>
